package com.info.test;

import static java.lang.System.out;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;

import com.info.pdf.base.MyMessageException;

/**
 * @Title: Main.java
 * @Description: TODO(添加描述)
 * @author H819
 * @date 2011-2-12
 * @version V1.0
 */
public class Main {

	private void gMailContact() {

	}

	private void test() {

		int count = 2499;
		float countTofloat = count * 1.0f;
		int step = Math.round(countTofloat / 100f);
		int progress = 0;
		int i = 1;

		while (progress < 100 && i <= count) {

			out.println("doSomething(): i = " + i);

			// 根据 i 的变化，分不同的情况，更新 progress ，和具体的业务没有关系
			// 为了进度条显示的更加合理，把任务数量分成了几个区间。
			// 否则，当任务很少的时候，进度条没有到 100%，任务已经完成了。
			if (count <= 100) {
				int divisor = 100 / count;

				if (i * divisor <= 100 && i <= count) {
					progress = i * divisor;
					out.println(" **************** update progress = "
							+ progress);

					if (i == count) {
						progress = 100;
						out.println(" **************** update progress = "
								+ progress);
					}
				}

			} else if (100 < count) {

				// 如果 progress 已经接近 100 ，但任务总数还没完成，则不更新 progress，继续处理任务。
				if (progress == 98 && i < count) {
					i++;
					continue;

				}

				if (i % step == 0) {// progress 增长 1
									// 的时机，经过四舍五入 Math.round
					progress++;
					out.println(" **************** update progress = "
							+ progress);
				}

				// 已经到达任务总数，而 progress 还没到 100，直接赋值 progress 为 100
				if (i == count) {
					progress = 100;
					out.println(" **************** update progress = "
							+ progress);
				}

			}

			i++;

		}
	}

	private void testFileNameList() {

		// 拷贝资源文件到临时目录

		String[] source = new String[] { "/pdfdecrypt/license.dat",
				"/pdfdecrypt/pdfdecrypt.exe", "/pdfdecrypt/SkinMagic.dll" };

		InputStream is = null;

		for (String s : source) {
			is = this.getClass().getResourceAsStream(s);
			if (is == null)
				return;

			String tempfilepath = SystemUtils.getJavaIoTmpDir()
					+ File.separator + s;
			File f = new File(tempfilepath);
			// 如果临时文件存在，直接返回其路径
			if (!f.exists() || FileUtils.sizeOf(f) == 0) {

				try {
					// 拷贝资源文件到临时文件夹
					FileUtils.copyInputStreamToFile(is, f);

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}
	}

	private void testCopy() {

		// 拷贝资源文件到临时目录
		String[] Resourcename = new String[] { "/pdfdecrypt/license.dat",
				"/pdfdecrypt/pdfdecrypt.exe", "/pdfdecrypt/SkinMagic.dll" };

		InputStream is = null;

		for (String s : Resourcename) {

			out.println("Resourcename name :" + s);

			is = this.getClass().getResourceAsStream(s);

			if (is == null) {
				try {
					throw new MyMessageException(s + " not exist.");
				} catch (MyMessageException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			String tempfilepath = SystemUtils.getJavaIoTmpDir()
					+ File.separator + s;

			out.println("tempfilepath :" + tempfilepath);

			File f = new File(tempfilepath);
			// 如果临时文件存在，直接返回其路径
			if (!f.exists() || FileUtils.sizeOf(f) == 0) {

				try {
					// 拷贝资源文件到临时文件夹
					FileUtils.copyInputStreamToFile(is, f);

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}

		String execommand = SystemUtils.getJavaIoTmpDir() + File.separator
				+ "/pdfdecrypt/pdfdecrypt.exe";

		out.println("execommand :" + execommand);
	}
	
	private void writeTest(){
		
		// 测试

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Main main = new Main();
		// main.gMailContact();

		String s = Main.class.getClassLoader().getResource("").getPath();
		//String path=ParsingXML.class.getClassLoader().getResource("").getPath(); 
		//out.println(s);
		
		main.writeTest();
		

		// out.println(FileUtils.sizeOf(new
		// File("D:\\download\\sae0"))/1000000.00);

		// out.println(FileUtils.sizeOf(new File("D:\\download\\sae0")));

		// out.println(Math.round(1.49));
		// out.println(Math.round(1.5));
		// out.println(Math.round(1.51));
		// out.println(Math.round(150*1.0f / 100f));

		// main.testFileNameList();

		//main.testCopy();

	}

}
